<?php
Prado::using('Application.common.common');
class ManageMailingList extends TPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		if (!$this->IsPostBack)
		{
			$this->Master->pageHead->Title = "Admin | Manage Mailing List";
			$this->AdminMenu->lnkManageMailingList->CssClass = "active";			
			$this->populateUsers();
			$this->populateUsers2();
		}
	}
	
	protected function populateUsers()
	{
		$criteria = new TActiveRecordCriteria;
		$criteria->Condition = "user_status >= 1 and receive_updates = 'Y'";				
		$criteria->OrdersBy["user_name"] = "asc";							
		$activeRecord = Prado::createComponent("UserRecord");
		$limit = null;
		$offset = null;
		$criteria->Limit = $limit;
		$criteria->Offset = $offset;
		$items = $activeRecord->finder()->findAll($criteria);
		$this->ItemList_Request->DataSource = $items;
		$this->ItemList_Request->dataBind();
		$this->lblTotal->Text="Newsletter Signed Up (" . count($items) . ")";		
		if (count($items) <= 0)
		{
			$this->lblTotal->Text="There is no user signed up for newsletter.";
			$this->lblTotal->Style="text-transform:lower; margin-left:40px;";
		}
	}
	
	protected function populateUsers2()
	{
		$criteria = new TActiveRecordCriteria;
		$criteria->Condition = "status = 1";				
		$criteria->OrdersBy["user_email"] = "asc";							
		$activeRecord = Prado::createComponent("NewsletterRecord");
		$limit = null;
		$offset = null;
		$criteria->Limit = $limit;
		$criteria->Offset = $offset;
		$items = $activeRecord->finder()->findAll($criteria);
		$this->ItemList2_Request->DataSource = $items;
		$this->ItemList2_Request->dataBind();
		$this->lblTotal2->Text="Non-user Signed Up (" . count($items) . ")";		
		if (count($items) <= 0)
		{
			$this->lblTotal2->Text="There is no user signed up for newsletter.";
			$this->lblTotal2->Style="text-transform:lower; margin-left:40px;";
		}
	}
	
	protected function btnGetEmail_Clicked($sender, $param)
	{
		try
		{
			$filecontent="LateNightDine Email List: ";
			
			$criteria = new TActiveRecordCriteria;
			$criteria->Condition = "user_status >= 1 and receive_updates = 'Y'";				
			$criteria->OrdersBy["user_name"] = "asc";							
			$activeRecord = Prado::createComponent("UserRecord");
			$mailingLists = $activeRecord->finder()->findAll($criteria);
			
			for($i=0;$i<count($mailingLists);$i++)
			{
				$filecontent .= $mailingLists[$i]->Email . ", ";
			}
			
			$filecontent = substr($filecontent, 0, strlen($filecontent) - 2);
			
			$fileName = "LND_Email_List_".date("d-m-Y",time()).".txt";
			$this->Response->appendHeader("Content-Type:plain/text");
			$this->Response->appendHeader("Content-Disposition:attachment;filename=$fileName");
			$this->Response->appendHeader("Cache-Control:max-age=0");
			$this->Response->appendHeader("Content-Transfer-Encoding: binary");			
			echo"$filecontent"; 
			exit();
		}
		catch(Exception $ex)
		{
			$this->Notice->Type = AdminNoticeType::Error;
			$this->Notice->Text = $this->Application->getModule("message")->translate("UNKNOWN_ERROR");
		}
	}
	
	protected function btnGetEmail2_Clicked($sender, $param)
	{
		try
		{
			$filecontent="LateNightDine Email List: ";
			
			$criteria = new TActiveRecordCriteria;
			$criteria->Condition = "status = 1";				
			$criteria->OrdersBy["user_email"] = "asc";							
			$activeRecord = Prado::createComponent("NewsletterRecord");
			$mailingLists = $activeRecord->finder()->findAll($criteria);
			
			for($i=0;$i<count($mailingLists);$i++)
			{
				$filecontent .= $mailingLists[$i]->Email . ", ";
			}
			
			$filecontent = substr($filecontent, 0, strlen($filecontent) - 2);
			
			$fileName = "LND_Email_List_".date("d-m-Y",time()).".txt";
			$this->Response->appendHeader("Content-Type:plain/text");
			$this->Response->appendHeader("Content-Disposition:attachment;filename=$fileName");
			$this->Response->appendHeader("Cache-Control:max-age=0");
			$this->Response->appendHeader("Content-Transfer-Encoding: binary");			
			echo"$filecontent"; 
			exit();
		}
		catch(Exception $ex)
		{
			$this->Notice->Type = AdminNoticeType::Error;
			$this->Notice->Text = $this->Application->getModule("message")->translate("UNKNOWN_ERROR");
		}
	}
}

?>